﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.WebControls;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class InstrumentReadRecordExtra : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }

        public Guid MaintainID
        {
            get
            {
                if (hiddenMaintainID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenMaintainID.Value);
            }
            set
            {
                hiddenMaintainID.Value = value.ToString();
            }
        }      

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["MaintainID"] != null)
                {
                    MaintainID = new Guid(Request.QueryString["MaintainID"]);
                    WsmMaintainBillEntity maintainEntity =  new SystemBll().Get<WsmMaintainBillEntity>(MaintainID);
                  
                    WsmTaskRegisterEntity taskEntity = new SystemBll().Get<WsmTaskRegisterEntity>(maintainEntity.RegisterID);
                    if (taskEntity.TaskStatus == 1) {
                        lnkbtnSaveAndClose.Visible = false;
                    }
                    List<WsmInstrumentAssemblyEntity> recordList = new SystemBll().GetAll<WsmInstrumentAssemblyEntity>(" and SerialNum = '" + maintainEntity.InstrumentSerialNum+ "'");
                    if (recordList != null && recordList.Count > 0) {
                        ReadNumType.Enabled = false;
                        ReadNumType.SelectedValue = recordList[0].ReadNumType.ToString();
                    }
                    List<WsmInstrumentRecordEntity> list = new SystemBll().GetAll<WsmInstrumentRecordEntity>(" and MaintainID='"+ MaintainID.ToString()+"'");
                    if (list != null && list.Count > 0) {
                        ReadNumType.SelectedValue = list[0].ReadNumType.ToString();
                        ReadNum.Value = System.Convert.ToDouble(list[0].TotalNum);
                        ReadNumAverageDaily.Value = System.Convert.ToDouble(list[0].DailyNum);
                        ReadNumDate.SelectedDate = System.Convert.ToDateTime(list[0].RecordDate);
                        lnkbtnSaveAndClose.Visible = false;
                    }
                    DataTable dtnearlyRecord = new WsmInstrumentRecordBll().GetInstrumentRecordByConfirmParms(maintainEntity.InstrumentSerialNum, taskEntity.InstrumentID, taskEntity.CustomerID);
                    if (dtnearlyRecord != null && dtnearlyRecord.Rows.Count > 0)
                    {
                        hiddenLastRecordNum.Text = dtnearlyRecord.Rows[0]["TotalNum"].ToString();
                        hiddenLastRecordDate.Text = dtnearlyRecord.Rows[0]["RecordDate"].ToString();
                        lastDailyNum.Text = dtnearlyRecord.Rows[0]["DailyNum"].ToString();
                    }
                    else
                    {
                        hiddenLastRecordNum.Text = "0";
                        hiddenLastRecordDate.Text = DateTime.Now.ToString();
                        lastDailyNum.Text = "0";
                    }
                }
            }
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            if (ReadNumDate.SelectedDate == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('请填写登记日期');", true);
                return;
            }
            if (ReadNumDate.SelectedDate.Value.Date.CompareTo(DateTime.Now.Date) > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('登记日期不能大于当前日期');", true);
                return;
            }
            WsmMaintainBillEntity maintainEntity = new SystemBll().Get<WsmMaintainBillEntity>(MaintainID);
            WsmTaskRegisterEntity taskEntity = new SystemBll().Get<WsmTaskRegisterEntity>(maintainEntity.RegisterID);
            
            WsmInstrumentRecordEntity entity = new WsmInstrumentRecordEntity();
            entity.RecordID = Guid.NewGuid();
            entity.Recorder = CurrentUserID;
            entity.RecordDate = ReadNumDate.SelectedDate.Value;
            entity.InstrumentID = taskEntity.InstrumentID;
            entity.CreatedBy = CurrentUserID;
            entity.CreatedOn = DateTime.Now;
            entity.ReadNumType = System.Convert.ToInt32(ReadNumType.SelectedValue);
            entity.Deleted = false;
            entity.MaintainID = MaintainID;
            entity.CustomerID = taskEntity.CustomerID;
            if (maintainEntity.InstrumentSerialNum == null || maintainEntity.InstrumentSerialNum == "")
                entity.SerialNum = DateTime.Now.ToString();
            else
                entity.SerialNum = maintainEntity.InstrumentSerialNum;
            if (ReadNumType.SelectedValue == "1")
            {
                if (System.Convert.ToDecimal(ReadNum.Value) <= System.Convert.ToDecimal(hiddenLastRecordNum.Text))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "saveerror", "alert('计数总数不能小于上次总数');", true);
                    return;
                }

                if (System.Convert.ToDecimal(ReadNum.Value) == 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('未输入有效的读数，无法保存');", true);
                    return;
                }
                entity.TotalNum = System.Convert.ToDecimal(ReadNum.Value);
                if (hiddenLastRecordNum.Text != "0")
                {
                    decimal lastTotal = System.Convert.ToDecimal(hiddenLastRecordNum.Text);
                    DateTime lastRecordDate = System.Convert.ToDateTime(hiddenLastRecordDate.Text);
                    int interval = entity.RecordDate.Date.Subtract(lastRecordDate.Date).Days;
                    decimal intevalRecord = System.Convert.ToDecimal(ReadNum.Value) - lastTotal;
                    decimal averRecord = intevalRecord / interval;
                    entity.DailyNum = averRecord;
                }
                else
                {
                    entity.DailyNum = System.Convert.ToDecimal(ReadNum.Value);
                }
            }          
            if (ReadNumType.SelectedValue == "2" )
            {
                if (System.Convert.ToDecimal(ReadNumAverageDaily.Value) == 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('未输入有效的读数，无法保存');", true);
                    return;
                }

                entity.DailyNum = System.Convert.ToDecimal(ReadNumAverageDaily.Value);
            }
            new SystemBll().Insert(entity);

            this.lnkbtnSaveAndClose.Visible = false;
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');", true);
        }       
    }
}
